//
//  TwitterPoster.m
//  HahnWinery
//
//  Created by HaiNguyenThanh on 4/19/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "TwitterPoster.h"
#import "AppDelegate_iPhone.h"


@implementation TwitterPoster
@synthesize username;
@synthesize loginDelegate;
@synthesize mOuathLogin;

- (id) init
{
	if (self=[super init])
	{
		mOuathLogin = [[OuathAuthViewController alloc]init];
	}
	return self;
}
- (NSString*)username
{
	return [mOuathLogin returnUserName];
}

- (void) logout
{
	NSLog(@"logout");
	[mOuathLogin logoutOfTwitter];
	[loginDelegate logOff];
	[[NSUserDefaults standardUserDefaults] setObject:NULL forKey:@"twitter_screenName"];
}
- (BOOL)isLogedIn
{
	if (mOuathLogin._engine == nil)
	{
		return NO;
	}
	
	return YES;
}
#pragma mark -
#pragma mark Twitter Delegate Methods
- (void)login:(id)delegate
{
	if(mOuathLogin == nil)
	{
		mOuathLogin = [OuathAuthViewController alloc] ;
	}
	if(delegate)
		mOuathLogin.delegate = self;
	//mOuathLogin.isAuthentc = FALSE;
	[mOuathLogin Login:delegate];
}
- (void) twit:(NSString*)text withLink:(NSString*)link forapp:(NSString *)twitAppname makeTiny:(BOOL)yesOrno delegate:(id)delegate
{
	if(mOuathLogin == nil)
	{
		mOuathLogin = [OuathAuthViewController alloc] ;
	}
	if(delegate)
	{
		mOuathLogin.delegate = delegate;
	}
	NSString *s = text;
	/*
	if (wine != nil)
	{
		if (text != NULL)
		{
			s= [NSString stringWithFormat:@"%@\n%@\n%@",wine.LabelTitle,wine.Varietal,text];
		}
		else
		{
			[NSString stringWithFormat:@"%@\n%@",wine.LabelTitle,wine.Varietal];
		}
	}
	else
	{
		s = text;
	}
	 */
	
	[mOuathLogin twit:s withLink:link forapp:twitAppname makeTiny:yesOrno :mainiPhoneAppDelegate.mainNavController];	
}
-(void)didAuthenticatedWithUserName:(NSString *)userName
{
	if (userName != NULL || userName != @"")
	{
		NSLog(@"user name = %@",userName);
	}
	username = [NSString stringWithFormat:@"%@", userName];
	[userName retain];
	[loginDelegate getUserInfoTwitter];
}
-(void)didAuthenticatedCancel
{
	
}
@end
